/* 
 * File:   Imagens.h
 * Author: Claudio
 *
 * Created on February 26, 2013, 1:21 AM
 */

#ifndef IMAGENS_H
#define	IMAGENS_H

#include <cstdlib>
#include <vector>
#include <string>
#include <cv.h>
#include <algorithm>
#include <utility>

using namespace std;

namespace AAM{


    
class Imagens{
public:
    Imagens(string filename, string date, string time){
//        this->image = img;
        priority = 1;
        score = (rand()%100)/10.;
        this->filename = filename;
        this->strdate = date;
        this->strtime = time;
        year=month=day=hour=min=sec=0;
        if(!date.empty() && !time.empty()){
            vector<string> dateSplitted;
            dateSplitted = split(strdate,':',dateSplitted);
            vector<string> timeSplitted;
            timeSplitted = split(strtime,':',timeSplitted);
            year = atoi(dateSplitted[0].c_str());
            month = atoi(dateSplitted[1].c_str());
            day = atoi(dateSplitted[2].c_str());
            hour = atoi(timeSplitted[0].c_str());
            min = atoi(timeSplitted[1].c_str());
            sec = atoi(timeSplitted[2].c_str());
        }
        notSet=true;
    }
    virtual ~Imagens(){
        
    }
    time_t getTime(){
        if(notSet){
            struct tm * timeinfo;
            time_t rawtime;
            time ( &rawtime );
            timeinfo = localtime ( &rawtime );
            timeinfo->tm_year = year-1900;
            timeinfo->tm_mon = month-1;
            timeinfo->tm_mday = day;
            timeinfo->tm_hour = hour;
            timeinfo->tm_min = min;
            timeinfo->tm_sec = sec;
            longtime = mktime(timeinfo);
            notSet=false;
        }
        return longtime;
    }
    string getFilename(){
        return filename;
    }
    void setDistanceTo(int pos,float distance){
        if(pos >= distances.size()){
            distances.resize(pos+1);
        }
        distances.at(pos)=distance;
    }
    float getDistanceTo(int pos){
        return distances.at(pos);
    }
    float getDiffTo(Imagens* img){
        vector<pair<Imagens*,float> >::iterator it;
        it = differences.begin();
        while(it!=differences.end()){
            if(img == (*it).first){
                return (*it).second;
            }
            
            it++;
        }
        return -1;
    }
    vector<Imagens*> getSmallestDifferences(float threshold){
        vector<Imagens*> smallest;
        vector<pair<Imagens*,float> >::iterator it;
        it = differences.begin();
        while(it!=differences.end()){
            if((*it).second < threshold){
                smallest.push_back((*it).first);
            }
            
            it++;
        }
        return smallest;
    }
    static bool comparaImagens(pair<Imagens*,float> a,pair<Imagens*,float> b){
        return a.second<b.second;
    }
    
    vector<Imagens*> getSmallestImages(int n){
        vector<Imagens*> smallest;
        sort(differences.begin(),differences.end(),comparaImagens);
        if(n < differences.size()){
            for(int a=0;a<n;a++){
                smallest.push_back(differences.at(a).first);
            }
        }
        return smallest;
    }
    void setDiffTo(Imagens* img, float diff){
        pair<Imagens*,float> p;
        p.first = img;
        p.second = diff;
        differences.push_back(p);
    }
    void addHistogram(CvHistogram* hist){
        histRegions.push_back(hist);
    }
    CvHistogram* getRegionHistogram(int region){
        if(region > histRegions.size() || region < 0)
            return NULL;
        else{
            return histRegions[region];
        }
    }
    int getPriority(){
        return priority;
    }
    void setPriority(int prio){
        priority = prio;
    }
    
    double getScore(){
        return score;
    }
    void setSession(int sess){
        session = sess;
    }
    int getSession(){
        return session;
    }
private:
//    IplImage *image;
    string filename,strdate,strtime;
    int year,month,day,hour,min,sec;
    vector<float> distances;
    vector<pair<Imagens*,float> > differences;
    time_t longtime;
    bool notSet;
    vector<CvHistogram*> histRegions;
    double score;
    int priority;
    int session;
    std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
    std::stringstream ss(s);
    std::string item;
    while(std::getline(ss, item, delim)) {
        elems.push_back(item);
    }
    return elems;
}
};

}

#endif	/* IMAGENS_H */

